package util;

import java.util.*;
import logic.cache.CacheCaller;

public class MyCollectionUtils {

    public static <R extends Object, O extends Object> List<R> convertList(List<O> oList, Converter<R,O> converter) {
        if (oList == null) {
            return null;
        }

        List<R> rList = new ArrayList<R>(oList.size());

        for (O o : oList) {
            rList.add(converter.convert(o));
        }

        return rList;
    }

    public static <R extends Object, O extends Object> Set<R> convertSet(Set<O> oSet, Converter<R,O> converter) {
        if (oSet == null) {
            return null;
        }

        Set<R> rSet = new HashSet<R>(oSet.size());

        for (O o : oSet) {
            rSet.add(converter.convert(o));
        }

        return rSet;
    }

    public static <K1 extends Object, V1 extends Object, K2 extends Object, V2 extends Object> Map<K1,V1> convertMap(Map<K2,V2> oMap, Converter<K1,K2> converterK, Converter<V1,V2> converterV) {
        if (oMap == null) {
            return null;
        }

        Map<K1,V1> rMap = new HashMap<K1, V1>(oMap.size());

        for (Map.Entry<K2,V2> o : oMap.entrySet()) {
            rMap.put(converterK.convert(o.getKey()), converterV.convert(o.getValue()));
        }

        return rMap;
    }

    public static <K extends Object, V1 extends Object, V2 extends Object> Map<K,V1> convertMapValues(Map<K,V2> oMap, Converter<V1,V2> converterV) {
        if (oMap == null) {
            return null;
        }

        Map<K,V1> rMap = new HashMap<K, V1>(oMap.size());

        for (Map.Entry<K,V2> o : oMap.entrySet()) {
            rMap.put(o.getKey(), converterV.convert(o.getValue()));
        }

        return rMap;
    }

    public static <V extends Object, K1 extends Object, K2 extends Object> Map<K1,V> convertMapKeys(Map<K2,V> oMap, Converter<K1,K2> converterK) {
        if (oMap == null) {
            return null;
        }

        Map<K1,V> rMap = new HashMap<K1, V>(oMap.size());

        for (Map.Entry<K2,V> o : oMap.entrySet()) {
            rMap.put(converterK.convert(o.getKey()), o.getValue());
        }

        return rMap;
    }
}
